package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author moonlan
 * @date 2020/10/18 下午12:23
 */
@Entity
@Table(name = "score")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update score set score_is_deleted = true where score_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "score_is_deleted = false")
@ApiModel("成绩")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Score  extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId", parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "SCORE")})
    @Column(name = "score_id")
    @ApiModelProperty(value = "score的id", position = 1)
    private String scoreId;

    @OneToOne(targetEntity = Student.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "score_student_sid", referencedColumnName = "user_sid")
    @ApiModelProperty(value = "学生ID", position = 3)
    private Student student;

    @OneToOne(targetEntity = StudentCourse.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "score_course_id", referencedColumnName = "student_course_id")
    @ApiModelProperty(value = "课程", position = 3)
    private StudentCourse course;

    @Column(name = "score_usual_score")
    @ApiModelProperty(value = "平时成绩", position = 4)
    private float scoreUsualScore;

    @Column(name = "score_exam_score")
    @ApiModelProperty(value = "考试成绩", position = 4)
    private float scoreExamScore;

    @ManyToOne(targetEntity = Term.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "score_term_id", referencedColumnName = "term_id")
    @ApiModelProperty(value = "学期id", position = 5)
    private Term term;

    @Column(name = "score_is_forbidden")
    @ApiModelProperty(value = "分数是否被禁用", position = 6)
    private boolean scoreIsForbidden = false;

    @Column(name = "score_is_deleted")
    @ApiModelProperty(value = "分数是否被删除", position = 7)
    private boolean scoreIsDeleted = false;

    public Score(String scoreId, Student student, StudentCourse course, float scoreUsualScore, float scoreExamScore, Term term, boolean scoreIsForbidden, boolean scoreIsDeleted) {
        this.scoreId = scoreId;
        this.student = student;
        this.course = course;
        this.scoreUsualScore = scoreUsualScore;
        this.scoreExamScore = scoreExamScore;
        this.term = term;
        this.scoreIsForbidden = scoreIsForbidden;
        this.scoreIsDeleted = scoreIsDeleted;
    }

    public Score() {
    }

    public static ScoreBuilder builder() {
        return new ScoreBuilder();
    }

    public String getScoreId() {
        return this.scoreId;
    }

    public Student getStudent() {
        return this.student;
    }

    public StudentCourse getCourse() {
        return this.course;
    }

    public float getScoreUsualScore() {
        return this.scoreUsualScore;
    }

    public float getScoreExamScore() {
        return this.scoreExamScore;
    }

    public Term getTerm() {
        return this.term;
    }

    public boolean isScoreIsForbidden() {
        return this.scoreIsForbidden;
    }

    public boolean isScoreIsDeleted() {
        return this.scoreIsDeleted;
    }

    public void setScoreId(String scoreId) {
        this.scoreId = scoreId;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public void setCourse(StudentCourse course) {
        this.course = course;
    }

    public void setScoreUsualScore(float scoreUsualScore) {
        this.scoreUsualScore = scoreUsualScore;
    }

    public void setScoreExamScore(float scoreExamScore) {
        this.scoreExamScore = scoreExamScore;
    }

    public void setTerm(Term term) {
        this.term = term;
    }

    public void setScoreIsForbidden(boolean scoreIsForbidden) {
        this.scoreIsForbidden = scoreIsForbidden;
    }

    public void setScoreIsDeleted(boolean scoreIsDeleted) {
        this.scoreIsDeleted = scoreIsDeleted;
    }

    public static class ScoreBuilder {
        private String scoreId;
        private Student student;
        private StudentCourse course;
        private float scoreUsualScore;
        private float scoreExamScore;
        private Term term;
        private boolean scoreIsForbidden;
        private boolean scoreIsDeleted;

        ScoreBuilder() {
        }

        public ScoreBuilder scoreId(String scoreId) {
            this.scoreId = scoreId;
            return this;
        }

        public ScoreBuilder student(Student student) {
            this.student = student;
            return this;
        }

        public ScoreBuilder course(StudentCourse course) {
            this.course = course;
            return this;
        }

        public ScoreBuilder scoreUsualScore(float scoreUsualScore) {
            this.scoreUsualScore = scoreUsualScore;
            return this;
        }

        public ScoreBuilder scoreExamScore(float scoreExamScore) {
            this.scoreExamScore = scoreExamScore;
            return this;
        }

        public ScoreBuilder term(Term term) {
            this.term = term;
            return this;
        }

        public ScoreBuilder scoreIsForbidden(boolean scoreIsForbidden) {
            this.scoreIsForbidden = scoreIsForbidden;
            return this;
        }

        public ScoreBuilder scoreIsDeleted(boolean scoreIsDeleted) {
            this.scoreIsDeleted = scoreIsDeleted;
            return this;
        }

        public Score build() {
            return new Score(scoreId, student, course, scoreUsualScore, scoreExamScore, term, scoreIsForbidden, scoreIsDeleted);
        }

        public String toString() {
            return "Score.ScoreBuilder(scoreId=" + this.scoreId + ", student=" + this.student + ", course=" + this.course + ", scoreUsualScore=" + this.scoreUsualScore + ", scoreExamScore=" + this.scoreExamScore + ", term=" + this.term + ", scoreIsForbidden=" + this.scoreIsForbidden + ", scoreIsDeleted=" + this.scoreIsDeleted + ")";
        }
    }
}
